# Python环境一键迁移脚本 - PowerShell版本
# 将Python用户包和conda缓存从C盘迁移到F盘

Write-Host "========================================" -ForegroundColor Green
Write-Host "Python环境迁移脚本 v1.0" -ForegroundColor Green
Write-Host "从C盘迁移到F盘" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

# 检查管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Write-Host "⚠️  建议以管理员身份运行此脚本以获得最佳效果" -ForegroundColor Yellow
    Write-Host "继续以当前权限运行..." -ForegroundColor Yellow
}

# 1. 创建目标目录结构
Write-Host "`n📁 创建F盘目录结构..." -ForegroundColor Cyan

$targetDirs = @(
    "F:\Python-Environment",
    "F:\Python-Environment\Roaming",
    "F:\Python-Environment\Roaming\Python",
    "F:\Python-Environment\conda-cache"
)

foreach ($dir in $targetDirs) {
    if (-not (Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
        Write-Host "   ✅ 创建: $dir" -ForegroundColor Green
    } else {
        Write-Host "   ℹ️  已存在: $dir" -ForegroundColor Yellow
    }
}

# 2. 检查源目录
$sourcePythonDir = "C:\Users\Administrator\AppData\Roaming\Python"
$sourceCondaCache = "C:\Users\Administrator\conda_pkgs"

Write-Host "`n🔍 检查源目录..." -ForegroundColor Cyan

if (Test-Path $sourcePythonDir) {
    $pythonSize = (Get-ChildItem $sourcePythonDir -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
    Write-Host "   📦 Python用户包: $([math]::Round($pythonSize, 2)) MB" -ForegroundColor Green
} else {
    Write-Host "   ⚠️  Python用户包目录不存在" -ForegroundColor Yellow
}

if (Test-Path $sourceCondaCache) {
    $cacheSize = (Get-ChildItem $sourceCondaCache -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
    Write-Host "   📦 Conda缓存: $([math]::Round($cacheSize, 2)) MB" -ForegroundColor Green
} else {
    Write-Host "   ⚠️  Conda缓存目录不存在" -ForegroundColor Yellow
}

# 3. 复制Python用户包
if (Test-Path $sourcePythonDir) {
    Write-Host "`n🚚 迁移Python用户包..." -ForegroundColor Cyan
    Write-Host "   源目录: $sourcePythonDir" -ForegroundColor Gray
    Write-Host "   目标目录: F:\Python-Environment\Roaming\Python" -ForegroundColor Gray
    
    try {
        Copy-Item -Path "$sourcePythonDir\*" -Destination "F:\Python-Environment\Roaming\Python" -Recurse -Force
        Write-Host "   ✅ Python用户包迁移完成" -ForegroundColor Green
    } catch {
        Write-Host "   ❌ Python用户包迁移失败: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# 4. 复制conda缓存
if (Test-Path $sourceCondaCache) {
    Write-Host "`n🚚 迁移Conda缓存..." -ForegroundColor Cyan
    Write-Host "   源目录: $sourceCondaCache" -ForegroundColor Gray
    Write-Host "   目标目录: F:\Python-Environment\conda-cache" -ForegroundColor Gray
    
    try {
        Copy-Item -Path "$sourceCondaCache\*" -Destination "F:\Python-Environment\conda-cache" -Recurse -Force
        Write-Host "   ✅ Conda缓存迁移完成" -ForegroundColor Green
    } catch {
        Write-Host "   ❌ Conda缓存迁移失败: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# 5. 设置环境变量
Write-Host "`n🔧 设置环境变量..." -ForegroundColor Cyan

try {
    if ($isAdmin) {
        # 系统级环境变量
        [Environment]::SetEnvironmentVariable("PYTHONUSERBASE", "F:\Python-Environment\Roaming\Python", "Machine")
        [Environment]::SetEnvironmentVariable("CONDA_PKGS_DIRS", "F:\Python-Environment\conda-cache", "Machine")
        Write-Host "   ✅ 系统级环境变量设置完成" -ForegroundColor Green
    } else {
        # 用户级环境变量
        [Environment]::SetEnvironmentVariable("PYTHONUSERBASE", "F:\Python-Environment\Roaming\Python", "User")
        [Environment]::SetEnvironmentVariable("CONDA_PKGS_DIRS", "F:\Python-Environment\conda-cache", "User")
        Write-Host "   ✅ 用户级环境变量设置完成" -ForegroundColor Green
    }
} catch {
    Write-Host "   ❌ 环境变量设置失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 6. 创建conda配置文件
Write-Host "`n📝 更新conda配置..." -ForegroundColor Cyan

$condarcPath = "C:\Users\Administrator\.condarc"
$condarcContent = @"
# Conda配置文件 - F盘优化版本
pkgs_dirs:
  - F:\Python-Environment\conda-cache
  - F:\ProgramData\anaconda3\pkgs

envs_dirs:
  - F:\ProgramData\anaconda3\envs

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - defaults

show_channel_urls: true
channel_priority: flexible
"@

try {
    $condarcContent | Out-File -FilePath $condarcPath -Encoding UTF8 -Force
    Write-Host "   ✅ conda配置文件已更新" -ForegroundColor Green
} catch {
    Write-Host "   ❌ conda配置文件更新失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 7. 创建启动脚本
Write-Host "`n🎯 创建启动脚本..." -ForegroundColor Cyan

$startupScript = @"
@echo off
title Python环境 - F盘优化版
set PYTHONUSERBASE=F:\Python-Environment\Roaming\Python
set CONDA_PKGS_DIRS=F:\Python-Environment\conda-cache
set PATH=F:\Python-Environment\Roaming\Python\Python310\Scripts;%PATH%
echo Python环境已切换到F盘
echo PYTHONUSERBASE: %PYTHONUSERBASE%
echo.
conda activate base
cmd /k
"@

try {
    $startupScript | Out-File -FilePath "F:\Python-Environment\start-python-env.bat" -Encoding ASCII -Force
    Write-Host "   ✅ 启动脚本已创建: F:\Python-Environment\start-python-env.bat" -ForegroundColor Green
} catch {
    Write-Host "   ❌ 启动脚本创建失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 8. 验证迁移
Write-Host "`n✅ 迁移完成！" -ForegroundColor Green
Write-Host "`n📋 下一步操作:" -ForegroundColor Yellow
Write-Host "1. 重启终端或重新登录系统" -ForegroundColor White
Write-Host "2. 运行验证命令: python -m site" -ForegroundColor White
Write-Host "3. 使用新启动脚本: F:\Python-Environment\start-python-env.bat" -ForegroundColor White
Write-Host "4. 确认迁移成功后，可删除C盘旧文件释放空间" -ForegroundColor White

Write-Host "`n🎉 Python环境迁移脚本执行完成！" -ForegroundColor Green
Read-Host "按回车键退出"